import { message, Button } from 'antd'
import React, { useState, useRef } from 'react'
import { PageHeaderWrapper } from '@ant-design/pro-layout'
import ProTable, { ProColumns, ActionType } from '@ant-design/pro-table'
import { connect, history } from 'umi'
import CreateForm from './components/CreateForm'
import { saveManagesInfo } from '../service'
import moment from 'moment'
import { jobLevelTypeList } from '@/utils/constant'
import Details from './components/Details'
import {queryBatchList} from "../../service";

const TableList = () => {
  const actionRef = useRef();
  const [done, setDone] = useState(false);
  const [visible, setVisible] = useState(false);
  const [current, setCurrent] = useState(undefined);
  const [loading, setLoading] = useState(false);
  const [detailsVisible,setDetailsVisible] = useState(false);
  const [id,setId] = useState(null);

  const columns = [
    {
      title: '序号',
      render:(text,record,index)=>`${index+1}`,
      hideInSearch: true,
    },
    {
      title: '晋升人字文',
      dataIndex: 'promotionCode',
    },
    {
      title: '业务类型',
      dataIndex: 'type',
      hideInSearch: true,
      render: (text, record) => {
        const res = jobLevelTypeList.find((item) => item.value === parseInt(text));
        return res ? res.label : text
      },
    },
    {
      title: '人数',
      dataIndex: 'number',
      hideInSearch: true,
    },
    {
      title: '操作人',
      dataIndex: 'applyer',
      hideInSearch: true,
    },
    {
      title: '操作日期',
      dataIndex: 'createTime',
      render: (text) => {
        return text ? moment(text).format('YYYY-MM-DD') : ''
      },
      hideInSearch: true,
    },
    {
      title: '操作',
      dataIndex: 'option',
      valueType: 'option',
      render: (item, record) => (
        <>
          <a onClick={() => details(record.id)}>详情</a>
        </>
      ),
    }
  ];

  const details = (id) => {
    setId(id);
    setDetailsVisible(true);
  };

  const handleCancel = () => {
    setCurrent(null);
    setVisible(false);
    setDetailsVisible(false);
  };

  const addResign = () => {
    setVisible(true);
  };

  const handleSubmit = (values) => {
    setLoading(true);
    saveManagesInfo(values).then((req)=>{
      setDone(false);
      setCurrent(null);
      setVisible(false);
      if(req.flag){
        message.success('晋级信息保存成功！');
        actionRef.current.reload();
      }else{
        message.error(`操作保存失败！${req.error}`);
      }
      setLoading(false);
    })
  };

  return (
    <PageHeaderWrapper>
      <ProTable
        actionRef={actionRef}
        rowKey="id"
        request={(params) => queryBatchList(params,1)}
        columns={columns}
        toolBarRender={(action, { selectedRows }) => [
          <Button type="primary" onClick={() => addResign(null)}>新建</Button>,
        ]}
      />
      <CreateForm
        values={{}}
        done={done}
        isShow={`profession`}
        current={current}
        visible={visible}
        loading={loading}
        onCancel={handleCancel}
        onSubmit={handleSubmit}
      />
      <Details
        visible={detailsVisible}
        id={id}
        onCancel={handleCancel}
      />
    </PageHeaderWrapper>
  )
}

export default connect(({  }) => ({
}))(TableList)
